#!/bin/sh
# this script will convert your wiki.d folder into normal txt2tags files
# 
# then run "txt2tags -t html --encoding utf8 export_main.t2t" and "txt2tags -t html --encoding utf8 export_blog.t2t"
#
# <!> at the moment it doesn't work with wikitop.d

cd wiki.d

if [ ! -d t2texport ]; then 
	mkdir t2texport
fi

if [ -f t2texport/export_main.t2t ]; then 
	rm t2texport/export_main.t2t
fi
	touch t2texport/export_main.t2t
	echo -e "\n\n\n" > t2texport/export_main.t2t

if [ -f t2texport/export_blog.t2t ]; then 
	rm t2texport/export_blog.t2t
fi
	touch t2texport/export_blog.t2t
	echo  -e "\n\n\n" > t2texport/export_blog.t2t

if [ -f t2texport/export_help.t2t ]; then 
	rm t2texport/export_help.t2t
fi
	touch t2texport/export_help.t2t
	echo  -e "\n\n\n" > t2texport/export_help.t2t



for pmfile in Main.*  
do
	echo -e "\n\n\n" > t2texport/$pmfile.t2t
	cat $pmfile | grep "text=" | awk -F'%0a' '{for(i=1;i<=NF;i++) printf "%s\n", $i}' | \
	sed s/text=//g | sed s/%25/%/g | sed s/%3c/"<"/g  >> t2texport/$pmfile.t2t
	echo "" >> t2texport/$pmfile.t2t
	echo "%!include: " ${pmfile}".t2t" >> t2texport/export_main.t2t
done

for pmfile in Blog.* 
do
	echo -e "\n\n\n" > t2texport/$pmfile.t2t
	cat $pmfile | grep "text=" | awk -F'%0a' '{for(i=1;i<=NF;i++) printf "%s\n", $i}' | \
	sed s/text=//g | sed s/%25/%/g | sed s/%3c/"<"/g |\
	sed s/"\[\[!sample\]\]"//g | sed s/"\[\[#blogit_entrybody\]\]"//g | sed s/"\[\[#blogit_\(.*\)\]\]"//g |\
	sed s/"(:entrytitle:\(.*\):)"/"== \1 =="/g | sed s/"(:include \(.*\):)"/"\%\!include: \1.t2t"/g | sed s/"(:\(.*\):)"//g  >> t2texport/$pmfile.t2t
	echo "" >> t2texport/$pmfile.t2t
	echo "%!include: " ${pmfile}".t2t" >> t2texport/export_blog.t2t
done


for pmfile in Help.*  
do
	echo -e "\n\n\n" > t2texport/$pmfile.t2t
	cat $pmfile | grep "text=" | awk -F'%0a' '{for(i=1;i<=NF;i++) printf "%s\n", $i}' | \
	sed s/text=//g | sed s/%25/%/g | sed s/%3c/"<"/g  >> t2texport/$pmfile.t2t
	echo "" >> t2texport/$pmfile.t2t
	echo "%!include: " ${pmfile}".t2t" >> t2texport/export_help.t2t
done
